﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using SSATool.Common.Util;
using SSATool.DL.Common;
using SSATool.DL.Common.Dto;

#endregion

namespace SSATool.DL.Server
{
    /// <summary>
    /// Represents the basic service instance.
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class ServiceAdminService : IServiceAdminService
    {
        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets a list of all service controllers.
        /// </summary>
        /// <returns>Returns the service list.</returns>
        public IList<ServiceController> GetServiceList()
        {
            using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
            {
                return ServiceContext.Current.ServiceProcessManager.GetServiceList();
            }
        }

        /// <summary>
        /// Gets the service with the given unique name.
        /// </summary>
        /// <param name="uniqueServiceName">Name of the service to retrieve.</param>
        /// <returns>Returns the retrieved service.</returns>
        [OperationBehavior(Impersonation = ImpersonationOption.Required)]
        public ServiceController GetService(string uniqueServiceName)
        {
            PreCondition.AssertNotNullOrEmpty(uniqueServiceName, "uniqueServiceName");

            using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
            {
                return ServiceContext.Current.ServiceProcessManager.GetService(uniqueServiceName);
            }
        }

        /// <summary>
        /// Sends the changes on a service back to the host.
        /// </summary>
        /// <param name="service">Specifies the service to update.</param>
        [OperationBehavior(Impersonation = ImpersonationOption.Required)]
        public void SetService(ServiceController service)
        {
            using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
            {
                ServiceContext.Current.ServiceProcessManager.SetService(service);
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
